﻿Public Partial Class ViewUserDetail
    Inherits BaseUserControl

    Public ReadOnly Property RequestedUsername() As String
        Get
            If Request.QueryString("username") IsNot Nothing Then
                Return Request.QueryString("username").ToString
            Else
                Return ""
            End If
        End Get
    End Property

    Private userRoles As New Hashtable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            BindData()
        End If
    End Sub

    Private Sub BindData()
        Try

            dateCreation.Enabled = False
            dateLastActivity.Enabled = False
            dateLastLockOut.Enabled = False
            dateLastLogin.Enabled = False
            dateLastPasswordChange.Enabled = False

            If Me.RequestedUsername.Trim.Length > 0 Then

                Dim membershipUser As MembershipUser = Membership.GetUser(Me.RequestedUsername)
                For Each role As String In Roles.GetRolesForUser(Me.RequestedUsername)
                    userRoles.Add(role, "")
                Next

                chkIsApproved.Checked = membershipUser.IsApproved
                chkIsLockedOut.Checked = membershipUser.IsLockedOut
                chkIsOnline.Checked = membershipUser.IsOnline

                dateCreation.SelectedDate = membershipUser.CreationDate
                dateLastActivity.SelectedDate = membershipUser.LastActivityDate
                dateLastLockOut.SelectedDate = membershipUser.LastLockoutDate
                dateLastLogin.SelectedDate = membershipUser.LastLoginDate
                dateLastPasswordChange.SelectedDate = membershipUser.LastPasswordChangedDate

                txtUsername.Text = Utility.GetString(membershipUser.UserName)
                txtPasswordQuestion.Text = Utility.GetString(membershipUser.PasswordQuestion)
                txtEmail.Text = Utility.GetString(membershipUser.Email)
                txtComment.Text = Utility.GetString(membershipUser.Comment)

                rptRoles.DataSource = Roles.GetAllRoles
                rptRoles.DataBind()

            End If
        Catch ex As Exception
            Me.RaiseError(ex)
        End Try
    End Sub

    Protected Function IsUserInRole(ByVal roleName As String) As Boolean
        Return userRoles.ContainsKey(roleName)
    End Function

    Public Overrides Sub Rebind()
        BindData()
    End Sub

    Protected Sub btnUpdateUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateUser.Click

        Try

            Dim outMessage As String = ""
            Dim membershipUser As MembershipUser = Membership.GetUser(Me.RequestedUsername)

            membershipUser.IsApproved = chkIsApproved.Checked
            membershipUser.Email = txtEmail.Text.Trim
            membershipUser.Comment = txtComment.Text.Trim
            Membership.UpdateUser(membershipUser)
            outMessage = "User " & Me.RequestedUsername & " has updated successfuly!<br />"

            'Updating Roles
            Dim roleNames As String = ""

            For Each dataRow As RepeaterItem In rptRoles.Items
                Dim chkRole As CheckBox = dataRow.FindControl("chkIsInRole")
                If chkRole.Checked Then
                    roleNames &= chkRole.Text & ";"
                End If
            Next

            'Removing all roles for the user
            Dim oldRoles() As String = Roles.GetRolesForUser(Me.RequestedUsername)
            If oldRoles.Length > 0 Then
                Roles.RemoveUserFromRoles(Me.RequestedUsername, oldRoles)
            End If

            If roleNames.Length > 0 Then
                'Adding new roles for the user
                roleNames = roleNames.Remove(roleNames.Length - 1, 1)
                Roles.AddUserToRoles(Me.RequestedUsername, roleNames.Split(";"))
            End If

            outMessage += "Roles for user " & Me.RequestedUsername & " has updated successfuly!"

            Me.Prompt(outMessage, UserControlPromptTypes.Successful)

        Catch ex As Exception

            Me.RaiseError(ex)

        End Try

    End Sub

End Class